/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author machine
 */
@Stateless
public class IdiomapeliculasFacade extends AbstractFacade<Idiomapeliculas> implements IdiomapeliculasFacadeLocal {
    @PersistenceContext(unitName = "videosJEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public IdiomapeliculasFacade() {
        super(Idiomapeliculas.class);
    }
        @Override
    public Integer contarIdiomasVideo(Integer idpeliculas) {
        
        Long rowCnt = (Long) em.createNativeQuery("SELECT count(1) FROM idiomapeliculas ip where ip.idpeliculas = "+idpeliculas+"  ").getSingleResult(); 
        
        return rowCnt.intValue();

    }

    @Override
    public List<Idiomapeliculas> buscarIdiomasVideo(String idpelicula, String ididioma) {
        
        String condicion = "";
        
        if( ididioma != null ){
            condicion += " and p.ididiomas.ididiomas = " + ididioma + " ";
        }
        
        if( idpelicula != null ){
            condicion += " and p.idpeliculas.idpeliculas = " + idpelicula + " ";
        }
        
        Query q = em.createQuery( "SELECT p FROM Idiomapeliculas p WHERE p.ididomapeliculas > 0 " + condicion + " ORDER BY p.idpeliculas " );
        return q.getResultList(); // or getSingleResult();
    }
}
